package top.cjwtx.holy.phone.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import top.cjwtx.holy.pojo.dto.UserDTO;
import top.cjwtx.holy.pojo.po.UserAccount;

/**
 * 基础控制器
 */
public class BaseController {

    //    protected static final int Page_Size = C.pageSize;

    protected static final int Page_Size = 6;

    /**
     * 获取Subject
     *
     * @return Subject
     */
    public Subject getSubject() {
        Subject subject =  SecurityUtils.getSubject();
        return subject;
    }

    /**
     * 获取用户基本信息
     *
     * @return
     */
    public UserDTO getUser() {
        //shiro只存储该用户当前登陆的账号信息，不会存储该用户关联的信息，所有accountList中只有一个账户信息（即当前登陆的账户）
        Object principal = getSubject().getPrincipal();
        return (UserDTO)principal;
    }

    /**
     * 获取当前用户登陆的账号信息
     *
     * @return
     */
    public UserAccount getUserAccount() {
        UserAccount userAccount = getUser().getUserAccount();
        return userAccount;
    }

    /**
     * 获取账号类型
     *
     * @return 账号类型
     */
    public Long getAccountType() {
        return getUserAccount().getType();
    }

    /**
     * 获取用户id
     *
     * @return 用户id
     */
    public Long getUserId() {
        return getUser().getUserId();
    }

    /**
     * 用户退出登陆
     */
    public void loginOut() {
        getSubject().logout();
    }

    /**
     * 用户账户脱敏方法
     *
     * @param userAccount
     * @return 非敏感信息
     */
    public static UserAccount desensitizationUserAccount(UserAccount userAccount) {
        userAccount.setPassword(null);
        userAccount.setSalt(null);
        userAccount.setCreateId(null);
        userAccount.setCreateTime(null);
        userAccount.setUpdateId(null);
        userAccount.setUpdateTime(null);
        return userAccount;
    }


}
